﻿using System;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.DomainServices.Client;

namespace MVVMtutorialAdvanced.Web
{
	public static class BindingConfiguration
	{
		public static void SetConfiguration
		(
			DomainContext domainContext,
			TimeSpan sendTimeout,
			TimeSpan closeTimeout,
			TimeSpan openTimeout,
			TimeSpan receiveTimeout,
			long maxReceivedMessageSize
		)
		{
			if (!DesignerProperties.IsInDesignTool)
			{
				//Timeout
				//-------------------------------------------------------------------------------------------------------
				//((WebDomainClient<IAuthenticationDomainServiceContract>)DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 1, 1);

				var channelFactoryProperty = domainContext.DomainClient.GetType().GetProperty("ChannelFactory");
				if (channelFactoryProperty == null)
					throw new InvalidOperationException("There is no 'ChannelFactory' property on the DomainClient.");

				var factory = (ChannelFactory)channelFactoryProperty.GetValue(domainContext.DomainClient, null);

				factory.Endpoint.Binding.SendTimeout = sendTimeout;
				factory.Endpoint.Binding.CloseTimeout = closeTimeout;
				factory.Endpoint.Binding.OpenTimeout = openTimeout;
				factory.Endpoint.Binding.ReceiveTimeout = receiveTimeout;


				//MessageSize
				//-------------------------------------------------------------------------------------------------------
				HttpTransportBindingElement httpTransportBindingElement;
				HttpsTransportBindingElement httpsTransportBindingElement;


				var customBinding = factory.Endpoint.Binding as CustomBinding;
				if (customBinding != null)
				{
					httpTransportBindingElement = customBinding.Elements.Find<HttpTransportBindingElement>();
					if (httpTransportBindingElement != null)
					{
						httpTransportBindingElement.MaxReceivedMessageSize = maxReceivedMessageSize;  //Default=2147483647
						httpTransportBindingElement.MaxBufferSize = (int)maxReceivedMessageSize;  //for buffered equals MaxReceivedMessageSize
						httpTransportBindingElement.TransferMode = TransferMode.Buffered;

						//var binaryMessageEncodingBindingElement = customBinding.Elements.Find<BinaryMessageEncodingBindingElement>();
						//var securityBindingElement = customBinding.Elements.Find<SecurityBindingElement>();

						return;
					}

					httpsTransportBindingElement = customBinding.Elements.Find<HttpsTransportBindingElement>();
					if (httpsTransportBindingElement != null)
					{
						httpsTransportBindingElement.MaxReceivedMessageSize = maxReceivedMessageSize;  //Default=2147483647
						httpsTransportBindingElement.MaxBufferSize = (int)maxReceivedMessageSize;  //for buffered equals MaxReceivedMessageSize
						httpsTransportBindingElement.TransferMode = TransferMode.Buffered;

						return;
					}

					return;
				}

				var basicHttpBinding = factory.Endpoint.Binding as BasicHttpBinding;
				if (basicHttpBinding != null)
				{
					basicHttpBinding.MaxReceivedMessageSize = maxReceivedMessageSize;
					basicHttpBinding.MaxReceivedMessageSize = (int)maxReceivedMessageSize;
					basicHttpBinding.TransferMode = TransferMode.Buffered;

					return;
				}
			}
		}
	}

	public sealed partial class DomainServiceBook
	{
		partial void OnCreated()
		{
			BindingConfiguration.SetConfiguration(
				this,
				new TimeSpan(0, 0, 50), new TimeSpan(0, 0, 50),
				new TimeSpan(0, 0, 50), new TimeSpan(0, 0, 50),
				int.MaxValue);
		}
	}
}
